package reesoft.idgenerator.service.idstore;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import reesoft.common.util.ApplicationContextUtil;

/**
 * Created by reesoft on 2017-08-28
 */
@Configuration
@ConfigurationProperties(prefix = "id.store.redisIdStore")
public class RedisIdStoreConfig
{
    /**
     * 在 Redis 中存储序号的键的前缀
     */
    private String keyPrefix = "seq_";

    public String getKeyPrefix()
    {
        return keyPrefix;
    }

    public void setKeyPrefix(String keyPrefix)
    {
        this.keyPrefix = keyPrefix;
    }

    public RedisTemplate<String, String> createRedisTemplate()
    {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(((RedisTemplate) ApplicationContextUtil.getBean("redisTemplate"))
                .getConnectionFactory());
        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }
}
